﻿//
// BooPolyLineGraph.xaml.cpp
// Implementation of the BooPolyLineGraph class
//

#include "pch.h"
#include "BooPolyLineGraph.xaml.h"
#include "PolyLineGraphGrid.xaml.h"
#include "PolyLineGraphEditPanel.xaml.h"
#include "PolyLineGraphData.h"
#include "Constants.h"

using namespace PolyLineGraph2;

using namespace Platform;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Controls::Primitives;
using namespace Windows::UI::Xaml::Data;
using namespace Windows::UI::Xaml::Input;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::UI::Xaml::Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236




BooPolyLineGraph::BooPolyLineGraph()
{
	InitializeComponent();
	InitializeLineGraphComponent();
	CreateDefaultGraph();
}

void BooPolyLineGraph::InitializeLineGraphComponent()
{
	graphPanel = ref new StackPanel();
	graphGrids = ref new PolyLineGraphGrid();

	rootContainer->Width = 800;
	rootContainer->Height = 600;
	int test= rootContainer->Height;
	graphPanel->Width=rootContainer->Width - 500;
	graphPanel->Height=rootContainer->Height - 300;
	int x = graphPanel->Width;
	int y = graphPanel->Height;
	graphPanel->Orientation=Orientation::Horizontal;
	graphPanel->Background = ref new SolidColorBrush(Windows::UI::Colors::White);

	rootContainer->Children->Append(graphGrids);
	//rootContainer->Children->Append(graphPanel);

	editPanel = ref new PolyLineGraphEditPanel(this);

	editPopup=ref new Popup();
	editPopup->Child=editPanel;
	editPopup->Width=Window::Current->Bounds.Width;
	editPopup->Height=Window::Current->Bounds.Height;

	//rootContainer->Width=400;
	//rootContainer->Height=300;
	rootContainer->ManipulationMode=ManipulationModes::TranslateX|ManipulationModes::TranslateY;
}

void BooPolyLineGraph::CreateDefaultGraph()
{
	graphDatas = ref new PolyLineGraphData();
	Windows::UI::Color color = Windows::UI::Colors::Red;
	graphDatas->addRow("England", color);
	int tt = 1;
	graphDatas->addRow("America", Windows::UI::Colors::Blue);

	ListCollection<double>^  testDatas = ref new ListCollection<double>;
	testDatas->Append(23);
	testDatas->Append(55);
	graphDatas->addColume("第一季度", testDatas);

	testDatas->Clear();
	testDatas->Append(35);
	testDatas->Append(45);
	graphDatas->addColume("第二季度", testDatas);

	testDatas->Clear();
	testDatas->Append(55);
	testDatas->Append(25);
	graphDatas->addColume("第三季度", testDatas);

	testDatas->Clear();
	testDatas->Append(75);
	testDatas->Append(25);
	graphDatas->addColume("第四季度", testDatas);

	CreateGraph();
	UpdateGraphGrid();
}

void BooPolyLineGraph::CreateGraph()
{
	//draw all the lines
	rootContainer->Children->Clear();
	rootContainer->Children->Append(graphGrids);

	double maxValue = graphDatas->getMaxValue();

	double graphWidthGap = (rootContainer->Width - LeftBlank - RightBlank) / (1 + graphDatas->columeNames->Size);
	double graphHeight = rootContainer->Height - TopBlank - BottomBlank;

	Windows::UI::Xaml::Shapes::Line^ line;

	Windows::UI::Xaml::Media::EllipseGeometry^ points;

	for(unsigned int  i = 1; i < graphDatas->pointValues1->Size; i++)
	{
		line = ref new Windows::UI::Xaml::Shapes::Line();
		line->X1 = LeftBlank + i * graphWidthGap;
		line->X2 = line->X1 + graphWidthGap;

		line->Y1 = TopBlank + graphHeight * (1 - graphDatas->pointValues1->GetAt(i - 1) / maxValue);
		line->Y2 = TopBlank + graphHeight * (1 - graphDatas->pointValues1->GetAt(i) / maxValue);

		line->StrokeThickness = 2;
		line->Stroke = ref new SolidColorBrush(graphDatas->rowColors->GetAt(0));

		Windows::UI::Xaml::Shapes::Path^ path = ref new Windows::UI::Xaml::Shapes::Path();
		points = ref new Windows::UI::Xaml::Media::EllipseGeometry();
		points->RadiusX = 3;
		points->RadiusY = 3;
		points->Center = Point(line->X2, line->Y2);
		path->Data = points;
		path->Fill = ref new SolidColorBrush(graphDatas->rowColors->GetAt(0));
		rootContainer->Children->Append(path);

		if(i == 1)
		{
			path = ref new Windows::UI::Xaml::Shapes::Path();
			points = ref new Windows::UI::Xaml::Media::EllipseGeometry();
			points->RadiusX = 3;
			points->RadiusY = 3;
			points->Center = Point(line->X1, line->Y1);
			path->Data = points;
			path->Fill = ref new SolidColorBrush(graphDatas->rowColors->GetAt(0));
			rootContainer->Children->Append(path);
		}

		rootContainer->Children->Append(line);

	}


	for(unsigned int  i = 1; i < graphDatas->pointValues2->Size; i++)
	{
		line = ref new Windows::UI::Xaml::Shapes::Line();
		line->X1 = LeftBlank + i * graphWidthGap;
		line->X2 = line->X1 + graphWidthGap;

		line->Y1 = TopBlank + graphHeight * (1 - graphDatas->pointValues2->GetAt(i - 1) / maxValue);
		line->Y2 = TopBlank + graphHeight * (1 - graphDatas->pointValues2->GetAt(i) / maxValue);

		line->StrokeThickness = 2;
		line->Stroke = ref new SolidColorBrush(graphDatas->rowColors->GetAt(1));

		Windows::UI::Xaml::Shapes::Path^ path = ref new Windows::UI::Xaml::Shapes::Path();

		points = ref new Windows::UI::Xaml::Media::EllipseGeometry();
		points->RadiusX = 3;
		points->RadiusY = 3;
		points->Center = Point(line->X2, line->Y2);
		path->Data = points;
		path->Fill = ref new SolidColorBrush(graphDatas->rowColors->GetAt(1));
		rootContainer->Children->Append(path);

		if(i == 1)
		{
			path = ref new Windows::UI::Xaml::Shapes::Path();
			points = ref new Windows::UI::Xaml::Media::EllipseGeometry();
			points->RadiusX = 3;
			points->RadiusY = 3;
			points->Center = Point(line->X1, line->Y1);
			path->Data = points;
			path->Fill = ref new SolidColorBrush(graphDatas->rowColors->GetAt(1));
			rootContainer->Children->Append(path);
		}

		rootContainer->Children->Append(line);
	}
}

void BooPolyLineGraph::UpdateGraphGrid(){
	graphGrids->UpdatePolyLineGraphGrid(rootContainer->Width, rootContainer->Height, graphDatas->getMaxValue(), graphDatas->columeNames->Size);
}

void PolyLineGraph2::BooPolyLineGraph::EditButtonClicked(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
	rootContainer->Visibility = Windows::UI::Xaml::Visibility::Collapsed;
	editPopup->IsOpen = true;
	editPanel->ApplyData(graphDatas);
}


void PolyLineGraph2::BooPolyLineGraph::RePaint(Platform::Object^ e)
{
	graphDatas = (PolyLineGraphData^) e;

	rootContainer->Visibility = Windows::UI::Xaml::Visibility::Visible;
	editPopup->IsOpen = false;

	CreateGraph();
}
